/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Display.java
 *
 * Created on Sep 7, 2009, 9:32:47 PM
 */

package matpred;

import java.awt.Color;
import java.awt.Component;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;

/**
 *
 * @author Demola
 */
public class Display extends javax.swing.JFrame {

    /** Creates new form Display */
    public Display(DefaultTableModel dtm) {
        initComponents();
        jTable1.setModel(dtm);
        setTable(dtm);
    }

    private void setTable(DefaultTableModel dtm){
        DefaultTableCellRenderer dtcr = new DefaultTableCellRenderer();
        dtcr.setHorizontalAlignment(JLabel.CENTER);
        dtcr.setBackground(new Color(0,0,0));
        dtcr.setForeground(new Color(255,255,255));

        for(int i = 0; i < jTable1.getColumnCount(); i++){
            jTable1.getColumnModel().getColumn(i).setPreferredWidth(250);
            jTable1.getColumnModel().getColumn(i).setHeaderRenderer(dtcr);
        }

        for(int i = 0; i < dtm.getColumnCount(); i++){
            if(dtm.getValueAt(0, i) == null) continue;
            if(dtm.getValueAt(0, i).getClass().isArray()){
                jTable1.getColumnModel().getColumn(i).setCellRenderer(new ColRenderer());
            }
            if(dtm.getValueAt(0, i).getClass().getName().equals("javax.swing.table.DefaultTableModel")){
                jTable1.getColumnModel().getColumn(i).setCellRenderer(new ColHeader());
            }
        }

        jTable1.setRowHeight(50);
        
        jTable1.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(jTable1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 849, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(14, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Display(null).setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables


    class ColHeader implements TableCellRenderer{

        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, final boolean isSelected, boolean hasFocus, int row, int column) {

            if(value == null){
                return new JTable();
            }
            DefaultTableModel tm = (DefaultTableModel) value;
            JTable cellTable = new JTable(tm){

                @Override
                public boolean isRowSelected(int row) {
                    return isSelected;
                }
            };
            for(int i = 0; i < cellTable.getColumnCount(); i++){
                jTable1.getColumnModel().getColumn(i).setPreferredWidth(20);
                jTable1.getColumnModel().getColumn(i).setMaxWidth(20);
            }
            cellTable.setBounds(10, 10, 10, 11);
            return cellTable;
        }
    }


    class ColRenderer implements TableCellRenderer{

        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, final boolean isSelected, boolean hasFocus, int row, int column) {

            if(value == null){
                return new JTable();
            }
            Object [] val = (Object[])value;
            JTable cellTable = new JTable(new DefaultTableModel(0, val.length)){
                @Override
                public boolean isRowSelected(int row) {
                    return isSelected;
                }
            };
            cellTable.setTableHeader(null);
            DefaultTableModel tm = (DefaultTableModel)cellTable.getModel();
            tm.addRow(val);
            return cellTable;
        }
    }
}
